package com.zpf.dao.Impl;

import com.zpf.dao.BookDao;
import com.zpf.domain.Book;
import com.zpf.domain.Page;
import com.zpf.util.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class BookDaoImpl implements BookDao {
    private QueryRunner queryRunner = new QueryRunner(DataSourceUtils.getDataSource());

    @Override
    public List<Book> selectAll() {
        try {
            return queryRunner.query("select * from book", new BeanListHandler<>(Book.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Book> selectAllByPage(Page page) {
        String sql = "select * from book limit ?, ?";
        try {
            List<Book> query = queryRunner.query(sql, new BeanListHandler<Book>(Book.class), page.getStartRows(), page.getPageSize());
        return query;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override//总条数
    public int selectAllByPageCount() {
        String sql = "select count(*) from book;";
        try {
            long query = queryRunner.query(sql, new ScalarHandler<Long>());
            return (int) query;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public void delete(int bookId) {
        String sql = "delete from book where id = ?";
        try {
            queryRunner.update(sql, bookId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public Book selectById(int bookId) {
        String sql = "select * from book where id = ?";
        try {
           return queryRunner.query(sql, new BeanHandler<>(Book.class), bookId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void update(Book book) {
        String sql = "update book set title=?,author=?,publicDate=?,publisher=?,isbn=?,price=?,picture=?,cid=? where id=?";
        Object[] params = {book.getTitle(),book.getAuthor(),book.getPublicDate(),book.getPublisher(),book.getIsbn(),book.getPrice(),book.getPicture(),book.getCid(),book.getId()};
        try {
            queryRunner.update(sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override//新增
    public void insert(Book book) {
        String sql = "insert into book values (null,?,?,?,?,?,?,?,?)";
        Object[] params = {book.getTitle(),book.getAuthor(),book.getPublicDate(),book.getPublisher(),book.getIsbn(),book.getPrice(),book.getPicture(),book.getCid()};
        try {
            queryRunner.update(sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}